public class OrderedTree {
    public static void main(String[] args) {
        //使用中序遍历完成节点的遍历---》顺序二叉树
        //第n个元素的左节点的下标是2*n+1；
        //第n个元素的右节点的下标是2*n+2
        //数组里第n个元素的父节点对应的数组下标是（n-1）/2
        int[] arr={1,2,3,4,5,6,7};
        TreeOrder treeOrder=new TreeOrder(arr);
        System.out.println("前序遍历：");
        treeOrder.PreOrderPrint(0);
        System.out.println("\n"+"中序遍历：");
        treeOrder.MidOrderPrint(0);
        System.out.println("\n"+"后序遍历：");
        treeOrder.PostOrderPrint(0);


    }
}

class TreeOrder{
    private int[] arr;
    public TreeOrder(int[] arr){
        this.arr=arr;
    }

    //前序遍历
    public void PreOrderPrint(int index){
        if(index>=arr.length){
            return;
        }
        System.out.print(arr[index]+" ");
        //向左遍历
        PreOrderPrint(2 * index + 1);

        //向右遍历
        PreOrderPrint(2 * index + 2);

    }
    //中序遍历
    public void MidOrderPrint(int index){
        if(index>=arr.length){
            return;
        }
        //向左遍历
        MidOrderPrint(2 * index + 1);
        System.out.print(arr[index]+" ");
        //向右遍历
        MidOrderPrint(2 * index + 2);

    }
    //后序遍历
    public void PostOrderPrint(int index){
        if(index>=arr.length){
            return;
        }
        //向左遍历
        PostOrderPrint(2 *index + 1);
        //向右遍历
        PostOrderPrint(2 * index + 2);
        System.out.print(arr[index]+" ");

    }

}
